#include "../Headers/dummy.h"

cluster*
DummyModule_cl_invoke_func(cluster* DummyModule_cl) {
    DEBUG("> DummyModule_cl_invoke_func()\n");
    DEBUG("< DummyModule_cl_invoke_func()\n");
    return DummyModule_cl;
}

cluster*
DummyPrint_cl_invoke_func(cluster* DummyPrint_cl) {
    DEBUG("> DummyPrint_cl_invoke_func()\n");
    DEBUG("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
    DEBUG("< DummyPrint_cl_invoke_func()\n");
    return DummyPrint_cl;
}

/*
void
DummyPrint_cl_destroy_func() {
    
}

cluster*
DummyInt_cl_invoke_func(cluster* DummyInt_cl) {
    
}

void
DummyInt_cl_destroy_func(cluster* DummyInt_cl) {
    
}

cluster*
DummyInt_cl_job_handler(cluster* DummyInt_cl) {
}
*/

cluster*
initialize_DummyStrFunc_cl() {
    cluster* retval;

    retval = cl_new();
    cl_set_name(retval, "DummyStrFunc_cl");

    return retval;
}

cluster*
initialize_DummyStr_cl() {
    cluster* retval;

    retval = cl_new();
    cl_set_name(retval, "DummyStr_cl");

    return retval;
}

/*
cluster*
initialize_DummyInt_cl() {
    
}
*/

/*
 * This assumes that the job has stored data in the void* data field as an
 * int. It converts that to a char* and stores it as as a Str_cl in the job_memos.
 */
void
DummyStrFunc_cl_job_handler(cluster* DummyStrFunc_cl, job_list* job) {
    // 1: Take a job out of the jobs list
    // 2: Find all the dependencies in the dep_list
    // 3: Compute the result
    // 4: Remove the job from the jobs list
    // 5: Place a memo in the job_memos list
    // 6: Exit. Return func? Probably none...
    cluster* cl;
    cluster* memo;
    //char* result;
    char buffer[12];
    int len;

    // An int should be no more than 10 chars long.
    for (len = 0 ; len < 12 ; len++)
        buffer[len] = '\0';

    cl = cl_get_dep(DummyStrFunc_cl, "param", job->caller, job->call_time);
    // With DummyInt_cl, void* data is an int*.
    len = sprintf(buffer, "%d", *(int*)(cl->data));

    memo = initialize_DummyStr_cl();
    memo->data = (void*)malloc(sizeof(char) * (strlen(buffer) + 1));
    strcpy(memo->data, buffer);

    // Now that the job is done, we need to remove this item from DummyStrFunc_cl's job_list
    // and place it on the job_memos list.
    cl_remove_job(&DummyStrFunc_cl->jobs, job);
    cl_add_job(&DummyStrFunc_cl->job_memos, job);
}

void
DummyPrint_cl_job_handler(cluster* DummyPrint_cl, job_list* job) {
    // 1: job is supplied.
    // 2: The caller is the Module. Thus, the dependencies should be set up
    //      before this is called.
    // 3: Pull the parameter (param?) out of the dependencies list
    // 4: Take the Str out of the job_memos list
    //    4a) In order to take the Str out of the job_memos list, the job
    //      needs to have been executed.
    //    4b) The job that needs to have been executed is the DummyStrFunc
    //      job, which transormed the Int into a Str.
    //    4c) The StrFunc is an attribute of the Int_cl. If it weren't an
    //      attribute, then DummyPrint_cl should instead use the StrFunc in
    //      the Object_cl, but that isn't coded yet.
    // 5: Once the Str is in hand, print it.
    //    5a) In the future, something additional will need to be done to
    //      assure correct ordering.
    // 6: Exit.

    DEBUG("> DummyPrint_cl_job_handler()\n");
    printf("%s\n", (char*)cl_get_dep(DummyPrint_cl, "param", job->caller, job->call_time)->data);
    DEBUG("< DummyPrint_cl_job_handler()\n");
}
